//HLSL
Shader "Pao/NormalEffect_URP"
{
    Properties
    {
        _MainTex("Main Texture", 2D) = "white" {}
        _NormalMap("Normal Map", 2D) = "black" {}
        _Color("Tint Color", Color) = (1, 1, 1, 1)
        _MinB("MinB", Range(0,1)) = 0.1
        _MaxB("MaxB", Range(0,1)) = 0.455

        _UVOffset("UVOffset", Range(0,1)) = 0
    }

    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent" }

        Pass
        {
            Name "Pass11"

            Cull Off
            ZWrite Off
            ZTest Always
            Blend SrcAlpha OneMinusSrcAlpha

            HLSLPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct Attributes
            {
                float4 positionOS : POSITION;
                float2 uv : TEXCOORD0;
                float3 normal : NORMAL;
            };

            struct Varyings
            {
                float2 uv : TEXCOORD0;
                float4 positionHCS : SV_POSITION;
                float3 normal : TEXCOORD1;
                float3 viewDir : TEXCOORD2;
            };

            TEXTURE2D(_MainTex);
            SAMPLER(sampler_MainTex);

            TEXTURE2D(_NormalMap);
            SAMPLER(sampler_NormalMap);
            

            CBUFFER_START(UnityPerMaterial)
                float4 _Color;
                float4 _MainTex_ST;
                float _MinB;
                float _MaxB;
                half _UVOffset;
            CBUFFER_END

            Varyings vert(Attributes IN)
            {
                Varyings OUT;
                OUT.normal = normalize(mul(IN.normal, (float3x3)UNITY_MATRIX_M));
                OUT.viewDir = normalize(_WorldSpaceCameraPos - TransformObjectToWorld(IN.positionOS));
                OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
                OUT.positionHCS = TransformObjectToHClip(IN.positionOS);
                return OUT;
            }

            half4 frag(Varyings IN) : SV_Target
            {
                half4 normalMap = SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, IN.uv);
                // half scale = (normalMap.b - _MinB)/(_MaxB - _MinB);
                // if(normalMap.b < _MinB || normalMap.b > _MaxB)
                // {
                //     // scale = 0;
                // }

                return half4(normalMap.xyz, 1);

                // return half4(0, 0, normalMap.b, 1);

                // half2 uvOffset = half2(normalMap.b,normalMap.b) * _UVOffset;

                half4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
                // col.rgb = col.rgb * (1-scale) + half3(1,1,1) * scale;

                return col;
                

                // return half4(normalDir,1);
                return half4(normalize(IN.normal),1);
                // return half4(0,0,normal.z,1);
            }

            ENDHLSL
        }
    }
}